package com.five.api.service.impl;

import com.five.api.dao.CourseMapper;
import com.five.api.dao.OrdersMapper;
import com.five.api.dao.UserMapper;
import com.five.api.service.OrdersService;
import com.five.entity.Course;
import com.five.entity.Orders;
import com.five.entity.User;
import com.five.exceptions.BusinessException;
import com.five.qo.PageQO;
import com.five.util.MySessionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * @author: star
 * @Date: 2021-05-21 14:34
 * 订单 service实现类
 */
@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CourseMapper courseMapper;

    private Logger logger = LoggerFactory.getLogger(OrdersServiceImpl.class);

    @Override
    public List<Orders> selectOrdersById(Long id) {
        List<Orders> orders = ordersMapper.selectOrdersById(id);
        return orders;
    }

    @Override
    public List<Orders> selectOrdersByPage(PageQO pageQO) {
        pageQO.setPage((pageQO.getPage() - 1) * pageQO.getPageSize());
        List<Orders> orders = ordersMapper.selectOrdersByPage(pageQO);
        return orders;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public long insOrder(Long courseId, HttpServletRequest request) {

        //todo 事务管理
        //根据用户id获取用户
        MySessionContext mySc = MySessionContext.getInstance();
        HttpSession session = mySc.getSession(request.getRequestedSessionId());
        User sessionUser = (User) session.getAttribute("User");

        User user = userMapper.selectUserById(sessionUser.getId());

        //根据课程id获取课程
        Course course = courseMapper.queryById(courseId);

        //判断用户余额是否足够
        if(user.getBalance().intValue() < course.getMoney().intValue()){
            throw new BusinessException("用户余额不足");
        }

        long index = 0;
        try {
            //修改用户余额
            BigDecimal balance = user.getBalance().subtract(course.getMoney());
            logger.info("balance:{}" + balance.intValue());
            user.setBalance(balance);

            userMapper.updateUser(user);

            //用户余额充足
            //生成订单
            Orders orders = new Orders();
            orders.setUserId(user.getId());
            //生成订单号
            orders.setOrderCode(UUID.randomUUID().getLeastSignificantBits());
            orders.setMoney(course.getMoney());
            orders.setPayment(0);
            orders.setCourseId(course.getId());

            index = ordersMapper.insOrders(orders);
        }catch (Exception e){
            throw new BusinessException("购买失败");
        }


        return index;
    }
}
